# -*- coding:utf-8 -*-
import requests
import json
from utils import cityCode
import time

def searchyq(name ):
    code = cityCode.getCityCode(name)
    print(code)
    if code :
        html = requests.get(url='https://i.snssdk.com/ncov/city_data/?recent_days=3&city_code=' + code + '&only_history=0')
        jsonarr = json.loads(html.content.decode('utf-8'))
        print(jsonarr)
        cd = jsonarr["city_data"]
        qystr = ''
        for i in range(0,3):
            qystr = qystr +  '\n' + cd.get("districts")[i].get("name") +': ' \
                    '\n新增【'+ cd["districts"][i]["current"].get("confirmed_incr","0") + '】 ' \
                    ' 无症状【'+ cd["districts"][i]["current"].get("asymptomatic_incr","0") + '】 ' \
                    '\n疫情场所【'+ cd["districts"][i]["current"].get("track_count","0") + '】 ' \
                    ' 中高风险【'+ cd["districts"][i]["current"].get("risk_area_count","0") + '】 ' \

        jststr = ''
        for i in range(0,3):
            jststr = jststr + '\n' + str(cd["histories"][i]["date"]) +': ' \
                 '\n新增【'+ str(cd["histories"][i].get("mainland_confirmed_incr")) + '】' \
                 ' 无症状【'+ str(cd["histories"][i].get("asymptomatic_incr")) + '】' \
                 '\n现有确诊【'+ str(cd["histories"][i].get("treating")) + '】' \

        xwstr = ''
        if jsonarr.get("tips") != '' :
            xwjson = json.loads(jsonarr.get("tips"))
            for xw in xwjson :
                xwstr = xwstr + '\n' \
                 '【'+ str(xw["title"]) + '】 ' \
                 '来源【'+ str(xw["user_name"]) + '】 ' \
                 '\n链接【'+ str(xw["url"]) + '】 ' \

                print(xw)

        time_local = time.localtime(cd["current"]["update_time"])
        dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

        Allresult = '====' + cd["name"]+ '疫情概览====' + '\n更新时间'+dt +  '' \
                    '\n新增本土【' + cd["current"]["mainland_confirmed_incr"] + '】' \
                    '新增无症状【' + cd["current"]["asymptomatic_incr"] + '】' \
                    '\n现有确诊【' + cd["current"]["treating"] + '】' \
                    '累计确诊【' + cd["current"]["confirmed"] + '】' \
                    '\n累计治愈【' + cd["current"]["cures"] + '】' \
                    ' 累计死亡【' + cd["current"]["deaths"] + '】' \
                    '\n疫情分布区域前三：'+ qystr +'' \
                    '\n近三天：' + jststr + '' \
                    '\n新闻：' + xwstr + '\n'
        print(Allresult)
        return Allresult



def searchyqDetail(name ):
    code = cityCode.getCityCode(name)
    print(code)
    if code :
        html = requests.get(url='https://i.snssdk.com/ncov/city_data/?recent_days=7&city_code=' + code + '&only_history=0')
        jsonarr = json.loads(html.content.decode('utf-8'))
        print(jsonarr)
        cd = jsonarr["city_data"]
        qystr = ''
        for bean in cd.get("districts"):
            qystr = qystr +  '\n' + bean.get("name") +': ' \
                    '\n新增【'+ bean["current"].get("confirmed_incr","0") + '】 ' \
                    ' 无症状【'+ bean["current"].get("asymptomatic_incr","0") + '】 ' \
                    '\n疫情场所【'+ bean["current"].get("track_count","0") + '】 ' \
                    ' 中高风险【'+ bean["current"].get("risk_area_count","0") + '】 ' \

        jststr = ''
        for bean in cd.get("histories"):
            jststr = jststr + '\n' + str(bean["date"]) +': ' \
                 '\n新增【'+ str(bean.get("mainland_confirmed_incr")) + '】' \
                 ' 无症状【'+ str(bean.get("asymptomatic_incr")) + '】' \
                 '\n现有确诊【'+ str(bean.get("treating")) + '】' \

        xwstr = ''
        if jsonarr.get("tips") != '' :
            xwjson = json.loads(jsonarr.get("tips"))
            for xw in xwjson :
                xwstr = xwstr + '\n' \
                 '【'+ str(xw["title"]) + '】 ' \
                 '来源【'+ str(xw["user_name"]) + '】 ' \
                 '\n链接【'+ str(xw["url"]) + '】 ' \

                print(xw)

        time_local = time.localtime(cd["current"]["update_time"])
        dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

        Allresult = '====' + cd["name"]+ '疫情详细====' + '\n更新时间'+dt +  '' \
                    '\n新增本土【' + cd["current"]["mainland_confirmed_incr"] + '】' \
                    '新增无症状【' + cd["current"]["asymptomatic_incr"] + '】' \
                    '\n现有确诊【' + cd["current"]["treating"] + '】' \
                    '累计确诊【' + cd["current"]["confirmed"] + '】' \
                    '\n累计治愈【' + cd["current"]["cures"] + '】' \
                    ' 累计死亡【' + cd["current"]["deaths"] + '】' \
                    '\n疫情分布区域全：'+ qystr +'' \
                    '\n近7天：' + jststr + '' \
                    '\n新闻：' + xwstr + '\n'
        print(Allresult)
        return Allresult


if __name__ == '__main__':
    searchyqDetail("北京")